public with sharing class DropBox_Authentication {
	public static String[] request_token(){
		Http http = new Http();
		HttpRequest req = new HttpRequest();
		
		DropBox_Connector_Setting__c DropBox = new DropBox_Connector_Setting__c();
		
		if([SELECT count() FROM DropBox_Connector_Setting__c WHERE Name = 'Salesforce Connector YOUR SL' AND Current_User_ID__c =: System.Userinfo.getUserId()] != 1){
			
			/* Select d.oauth_token_secret1__c, d.oauth_token1__c, d.access_token_secret__c, d.access_token__c, d.SetupOwnerId, d.Name, d.Id, d.Current_User_ID__c, d.Authentication_Website__c, d.Application_Secret__c, d.Application_Key__c From DropBox_Connector_Setting__c d*/
			DropBox.Current_User_ID__c = System.Userinfo.getUserId();
			DropBox.Application_Key__c = 'r33huo1lbln9f6r';
			DropBox.Application_Secret__c = '41wzp91k7xda6a2';
			DropBox.Name = 'Salesforce Connector YOUR SL';
			insert DropBox;
		}else{
			DropBox = [SELECT Current_User_ID__c, Application_Key__c, Application_Secret__c, Name FROM DropBox_Connector_Setting__c WHERE Name = 'Salesforce Connector YOUR SL' AND Current_User_ID__c =: System.Userinfo.getUserId() LIMIT 1];
		}
		
		String sAutho = 'OAuth' + ' '; // sets the OAuth method
		sAutho += 'oauth_consumer_key' + '=' + DropBox.Application_Key__c + ', '; // consumer key offered by DropBox
		sAutho += 'oauth_signature_method' + '=' + '"PLAINTEXT"' + ', '; // easiest method is PLAINTEXT
		sAutho += 'oauth_version' + '=' + '"1.0"' + ', '; // easiest Version for OAuth is 1.0
		sAutho += 'oauth_signature' + '=' + DropBox.Application_Secret__c + '&'; // signature is the Application sectret offered by DropBox always add a Ampersand to the app secret
		
		req.setHeader('Authorization',sAutho);
		req.setEndpoint('https://api.dropbox.com/1/oauth/request_token');
		req.setMethod('POST');
		
		//System.debug('*** req *******' + req.getHeader('oauth_signature'));
		
		HTTPResponse res = http.send(req);
		
		//System.debug('*** res Status *******' +res.getStatus());
	     /* Test response
		 for(String sHeader : res.getHeaderKeys()){
		 	System.debug('*** res HeaderKeys*******' + sHeader);
		 	if(sHeader != NULL){
		 		System.debug('*** res Header Value*******' + res.getHeader(sHeader));
		 	}
		 }
		 */
		 
		 //System.debug('*** res Body *******' + res.getBody());
		 
		 if(!res.getBody().contains('error')){
		 	String sResponseBody = res.getBody();
		 	String[] listResponses1 = sResponseBody.split('&');
		 	String[] listResponses2 = listResponses1[0].split('=');
		 	String[] listResponses3 = listResponses1[1].split('=');
		 	
		 	DropBox.oauth_token1__c = listResponses3[1];
		 	DropBox.oauth_token_secret1__c = listResponses2[1];
		 	
		 	String oauth_token_secret = listResponses2[1];
		 	String oauth_token = listResponses3[1];
		 	System.debug('*** res oauth_token *******' + oauth_token);
		 	System.debug('*** res oauth_token_secret *******' + oauth_token_secret);
		 	
		 	update DropBox;
		 	
		 	return new List<String>{oauth_token,oauth_token_secret};
		 }else{
		 	return null;
		 }// if(!res.getBody().contains('error'))
	}// public static void request_token()
	
	public static List<String> authorize(){
		List<String> listOAuths = request_token();
		// listOAuths[0] = oauth_token
		// listOAuths[1] = oauth_token_secret
		
		DropBox_Connector_Setting__c DropBox = new DropBox_Connector_Setting__c();
		
		if([SELECT count() FROM DropBox_Connector_Setting__c WHERE Name = 'Salesforce Connector YOUR SL' AND Current_User_ID__c =: System.Userinfo.getUserId()] != 1){
			
			/* throw errer */
		}else{
			DropBox = [SELECT Current_User_ID__c, oauth_token1__c, oauth_token_secret1__c, Application_Key__c, Application_Secret__c, Name FROM DropBox_Connector_Setting__c WHERE Name = 'Salesforce Connector YOUR SL' AND Current_User_ID__c =: System.Userinfo.getUserId() LIMIT 1];
		}
		
		
		Http http = new Http();
		HttpRequest req = new HttpRequest();
		req.setEndpoint('https://www.dropbox.com/1/oauth/authorize');
		req.setMethod('GET');
		req.setHeader('oauth_token',DropBox.oauth_token1__c);
		req.setHeader('oauth_callback','');
		HTTPResponse res = http.send(req);
		System.debug('*** res Status *******' +res.getStatus());
		/*
		// This is the URL to allow the App for the current user
		// you need it to get access token
		// https://www.dropbox.com/1/oauth/authorize?oauth_token=<type your oauth token here in>&oauth_callback=https://www.dropbox.com/
		*/
		DropBox.Authentication_Website__c = 'https://www.dropbox.com/1/oauth/authorize?oauth_token=' + DropBox.oauth_token1__c + '&oauth_callback=https://www.dropbox.com/';
		
		return listOAuths;
	}
	
	
}